package com.atguigu.gmall.sms.service.impl;

import com.atguigu.gmall.sms.dao.SkuFullReductionDao;
import com.atguigu.gmall.sms.dao.SkuLadderDao;
import com.atguigu.gmall.sms.entity.SkuFullReductionEntity;
import com.atguigu.gmall.sms.entity.SkuLadderEntity;
import com.atguigu.gmall.sms.vo.SaleVO;
import com.atguigu.gmall.sms.vo.SkuSaleVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.core.bean.PageVo;
import com.atguigu.core.bean.Query;
import com.atguigu.core.bean.QueryCondition;

import com.atguigu.gmall.sms.dao.SkuBoundsDao;
import com.atguigu.gmall.sms.entity.SkuBoundsEntity;
import com.atguigu.gmall.sms.service.SkuBoundsService;
import org.springframework.transaction.annotation.Transactional;

@Service("skuBoundsService")
public class SkuBoundsServiceImpl extends ServiceImpl<SkuBoundsDao, SkuBoundsEntity> implements SkuBoundsService {

    @Autowired
    private SkuFullReductionDao skuFullReductionDao;

    @Autowired
    private SkuLadderDao skuLadderDao;

    @Override
    public PageVo queryPage(QueryCondition params) {
        IPage<SkuBoundsEntity> page =
            this.page(new Query<SkuBoundsEntity>().getPage(params), new QueryWrapper<SkuBoundsEntity>());

        return new PageVo(page);
    }

    /**
     * sms提供个体pms营销保存接口
     * 
     * @param skuSaleVO
     */
    @Override
    @Transactional
    public void saveSkuSaleInfo(SkuSaleVO skuSaleVO) {
        // 3.1积分优惠
        SkuBoundsEntity skuBoundsEntity = new SkuBoundsEntity();
        BeanUtils.copyProperties(skuSaleVO, skuBoundsEntity);
        // 数据库保存的是整数0-15，页面绑定的是0000-1111
        List<Integer> work = skuSaleVO.getWork();
        if (work.isEmpty()) {
            // 将二进制转为十进制保存到数据库
            skuBoundsEntity.setWork(work.get(0) * 8 + work.get(1) * 4 + work.get(2) * 2 + work.get(3));
        }
        this.save(skuBoundsEntity);

        // 3.2满减优惠
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuSaleVO, skuFullReductionEntity);
        skuFullReductionEntity.setAddOther(skuSaleVO.getFullAddOther());
        this.skuFullReductionDao.insert(skuFullReductionEntity);

        // 数量折扣
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        BeanUtils.copyProperties(skuSaleVO, skuLadderEntity);
        skuLadderEntity.setAddOther(skuSaleVO.getLadderAddOther());
        this.skuLadderDao.insert(skuLadderEntity);
    }

    /**
     * 获取sku的促销信息
     * 
     * @return
     */
    @Override
    public List<SaleVO> querySalesBySkuId(Long skuId) {
        List<SaleVO> saleVos = new ArrayList<>();

        // 查询积分信息
        SkuBoundsEntity skuBoundsEntity = this.getOne(new QueryWrapper<SkuBoundsEntity>().eq("sku_id", skuId));
        if (skuBoundsEntity != null) {
            SaleVO saleVO = new SaleVO();
            saleVO.setType("积分");
            StringBuilder builder = new StringBuilder();
            if (skuBoundsEntity.getGrowBounds() != null && skuBoundsEntity.getGrowBounds().intValue() > 0) {
                builder.append("成长积分送： " + skuBoundsEntity.getGrowBounds());
            }
            if (StringUtils.isNotBlank(builder)) {
                builder.append(" , ");
            }
            if (skuBoundsEntity.getBuyBounds() != null && skuBoundsEntity.getBuyBounds().intValue() > 0) {
                builder.append(" 赠送积分送" + skuBoundsEntity.getBuyBounds());
            }
            saleVO.setDesc(builder.toString());
            saleVos.add(saleVO);
        }

        // 查询打折信息
        SkuLadderEntity skuLadderEntity =
            this.skuLadderDao.selectOne(new QueryWrapper<SkuLadderEntity>().eq("sku_id", skuId));
        if (skuLadderEntity != null) {
            SaleVO ladderVo = new SaleVO();
            ladderVo.setType("打折");
            ladderVo.setDesc("满" + skuLadderEntity.getFullCount() + "件，打"
                + skuLadderEntity.getDiscount().divide(new BigDecimal(10)) + "折");// divide()为除法
            saleVos.add(ladderVo);
        }

        // 查询满减信息
        SkuFullReductionEntity skuFullReductionEntity =
            this.skuFullReductionDao.selectOne(new QueryWrapper<SkuFullReductionEntity>().eq("sku_id", skuId));
        if (skuFullReductionEntity != null) {
            SaleVO reductionVO = new SaleVO();
            reductionVO.setType("满减");
            reductionVO
                .setDesc("满" + skuFullReductionEntity.getFullPrice() + "减" + skuFullReductionEntity.getReducePrice());
            saleVos.add(reductionVO);
        }

        return saleVos;
    }

}